/**  
 * All rights Reserved, Designed By www.beonelot.com
 * @Package: com.jinxin.platform.eleinspect.controller
 * @Filename: EleinspectItemsController.java
 * @Description: TODO(EleinspectItemsController)
 * @author: 金鑫智慧
 * @date: 2020-03-30
 * @version: V1.0
 * @Copyright: 2020 www.beonelot.com Inc. All rights reserved.
 * 注意：本内容仅限于重庆金鑫智慧科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
package com.jinxin.platform.cdelecinspect.controller;


import com.jinxin.platform.cdelecinspect.mapper.pojo.EleinspectItems;
import com.jinxin.platform.cdelecinspect.service.EleinspectItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO(EleinspectItemsController)
 * @Classn: EleinspectItemsController
 * @author Lynn
 * @date 2020-03-30
 */
@RestController
@RequestMapping("/eleinspect/item")
public class EleinspectItemsController {
    @Autowired
    EleinspectItemsService eleinspectItemsService;

    @RequestMapping("/getItemTable")
    public Map<String,Object> getPointTable(Integer page, Integer limit,String itemsName,String beginDate,String endDate){
        HashMap<String,Object> param = new HashMap<>();
        param.put("page", page );
        param.put("limit", limit);
        if (!StringUtils.isEmpty(itemsName)){
            param.put("itemsName", itemsName);
        }
        if (!StringUtils.isEmpty(beginDate)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try{
                Date date = simpleDateFormat.parse(beginDate);
                param.put("beginDate", date);
            }catch (ParseException e){
                e.printStackTrace();
            }
        }
        if (!StringUtils.isEmpty(endDate)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try{
                Date date = simpleDateFormat.parse(endDate);
                param.put("endDate", date);
            }catch (ParseException e){
                e.printStackTrace();
            }
        }
        List<EleinspectItems> allPoint = eleinspectItemsService.listPage(param);
        int pageCount = eleinspectItemsService.listPageCount(param);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","请求成功");
        resultMap.put("data",allPoint);
        resultMap.put("count",pageCount);
        return resultMap;
    }

    @RequestMapping("/saveItemInfo")
    public Map<String,Object> savePointInfo(EleinspectItems eleinspectItems,int editIndex){
        Map<String,Object> resultMap = new HashMap<>();
        if (editIndex == 1){
            eleinspectItemsService.updateByKey(eleinspectItems);
            resultMap.put("code",0);
            resultMap.put("msg","编辑成功");
        }else{
            eleinspectItems.setItemsCreateTime(new Date());
            eleinspectItemsService.insert(eleinspectItems);
            resultMap.put("code",0);
            resultMap.put("msg","添加成功");
        }
        return resultMap;
    }

    @RequestMapping("/deleteItem")
    public Map<String,Object> deleteItem(String itemsId){
        Map<String,Object> resultMap = new HashMap<>();
        eleinspectItemsService.deleteByKey(itemsId);
        resultMap.put("code",0);
        resultMap.put("msg","删除成功");
        return resultMap;
    }
}
